//
//  Producer.java
//  ics314-calendar
//
//  Created by ICS314 Team Mississippi  on 7/30/14.
//  Copyright (c) 2014 JinHao Chen, Christian Caluya,Kevin Fujikawa. All rights reserved.
//
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import javax.swing.JOptionPane;


public class Producer {
	/**
	 * Produce the ics file after catch the user input
	 * @param  classification  classification for the ics file (RFC5545 format)
	 * @param  priority  priority for the ics file (RFC5545 format)
	 * @param  timeZone	 time zone for the user selection (RFC5545 format)
	 * @param  location   location that user input (RFC5545 format)
	 * @param  repeatSelector  is the recurring event or not 
	 * @param  repeatBy  the string for the recurring event {DAILY,WEEKLY,MONTHLY,YEARLY}
	 * @param  dt_start  the start date and time of the event
	 * @param  dt_end  the end date and time of the event
	 * @param  summary  the summary of the event
	 * @return  void
	 * @throws  FileNotFoundException  when the file can't create or error
	 * @throws  IOException  when the file write error
	 */
	public static void init(String classification, String priority, String timeZone, String location,String repeatSelector,String repeatBy, String dt_start, String dt_end, String summary) {
		FileOutputStream fos;	
		String TZOffSetTo = Judgment.convertTZOffSetFrom(timeZone);
		String TZOffSetFrom = Judgment.convertTZOffSetTo(timeZone);
		String repeatFREQ = Judgment.getRepeatFREQ(repeatSelector);
		String s = String.format(     
			       "BEGIN:VCALENDAR\n"+
			       "PRODID:-//Apple Inc.//Mac OS X 10.9.4//EN\n" +
			       "VERSION:2.0\n"+
			       "BEGIN:VTIMEZONE\n" +
			       "TZID:" +timeZone+"\n"+
			       "BEGIN:STANDARD\n" +
			       "DTSTART:19670101T000000\n" +
			       "TZOFFSETFROM:"+ TZOffSetFrom + "\n" +
			       "TZOFFSETTO:" + TZOffSetTo + "\n" +
			       "TZNAME:EST\n" +
			       "END:STANDARD\n" +
			       "BEGIN:DAYLIGH\n" +
			       "DTSTART:19670101T000000\n" +
			       "TZOFFSETFROM:" + TZOffSetTo + "\n" +
			       "TZOFFSETTO:" + TZOffSetFrom + "\n" +
			       "TZNAME:EDT\n" +
			       "END:DAYLIGHT\n" +
			       "END:VTIMEZONE\n" +
			       "BEGIN:VEVENT\n"+
			       "LOCATION:" +location +"\n" +
			       "GEO:37.386013;-122.082932\n" + // This is just an example, need to change
			       "CLASS:" + classification + "\n" +
			       "PRIORITY:" + priority + "\n" +
			       "DTSTART;TZID="+ timeZone + ":" + dt_start + "\n" +
			       "RRULE:FREQ=" + repeatFREQ + ";" + repeatBy +"\n" +
			       "DTEND;TZID="+ timeZone + ":" + dt_end + "\n" +
			       "SUMMARY:"+ summary +"\n" +
			       "END:VEVENT\n"+
			       "END:VCALENDAR");
		try {
			fos = new FileOutputStream("testing.ics");  
			OutputStreamWriter osw=new OutputStreamWriter(fos);
			BufferedWriter bw=new BufferedWriter(osw);
			bw.write(s);
			bw.flush();
			bw.close();
			JOptionPane.showMessageDialog(null,
					"You .ics file is at \n"+new File("testing.ics").getAbsolutePath(), 
					"Success!", 
					JOptionPane.OK_OPTION); 
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

}
